﻿using LineOfBusiness.Core.Helper;
using LineOfBusiness.DataAccess;
using LineOfBusiness.Poco;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LineOfBusiness
{
    public partial class ReportViewer : System.Web.UI.Page
    {
        private string CreateEMFDeviceInfo(LocalReport _LocalReport)
        {
            PaperSize paperSize = _LocalReport.GetDefaultPageSettings().PaperSize;
            Margins margins = _LocalReport.GetDefaultPageSettings().Margins;

            // The device info string defines the page range to print as well as the size of the page.
            // A start and end page of 0 means generate all pages.
            return string.Format(
                "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
                ToInches(margins.Top),
                ToInches(margins.Left),
                ToInches(margins.Right),
                ToInches(margins.Bottom),
                ToInches(paperSize.Height),
                ToInches(paperSize.Width));
        }

        private static string ToInches(int hundrethsOfInch)
        {
            double inches = hundrethsOfInch / 100.0;
            return inches.ToString() + "in";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadReport();
           
            this.NoData.Value = ReportViewer1.NoData ? "1" : "0";
        }

        private LocalReport LoadReport()
        {
            string Module = Request.QueryString["Module"];
            string ReportName = Request.QueryString["ReportName"];
            string EntityName = Request.QueryString["EntityName"];
            string FormTitle = Request.QueryString["FormTitle"];
            string Predicate = Request.QueryString["Predicate"];
            string DataValue = Request.QueryString["DataValue"];
            string Dynamic = Request.QueryString["Dynamic"];
            string Print = Request.QueryString["Print"];

            if (!IsPostBack)
            {
                if (!string.IsNullOrWhiteSpace(EntityName))
                {
                    string report = HttpContext.Current.Server.MapPath("~");
                    string path = "";
                    if (System.IO.File.Exists(report + "\\Reports\\Custom\\" + Module + "\\" + ReportName + ".rdlc"))
                    {
                        path = report + "\\Reports\\Custom\\" + Module + "\\" + ReportName + ".rdlc";

                    }
                    else
                    {
                        path = HttpContext.Current.Server.MapPath("~/bin/Reports") + "\\" + Module + "\\" + ReportName + ".rdlc";
                    }
                    using(EFDALContainer dal = new EFDALContainer())
                    {
                        var setup = dal.Repository.GetOne<AD_tblSetupCompany>(o=>1==1);
                        if(setup!=null)
                        {
                            ReportViewer1.ReportFormating.CompanyName = setup.CompanyName;
                            ReportViewer1.ReportFormating.CompanyAddress = setup.Address;
                            ReportViewer1.ReportFormating.Logo = setup.LogoFile;
                            
                        }
                        else
                        {
                            ReportViewer1.ReportFormating.CompanyName = "Danh Hien";
                            ReportViewer1.ReportFormating.CompanyAddress = "15 Thu Khoa Huan";
                        }

                    }
                    

                    return ReportViewer1.LoadReport(EntityName, path, FormTitle, Predicate, DataValue, Request, Module, ReportName);
                }
                else
                {
                    if (!string.IsNullOrEmpty(Dynamic))
                    {
                        return null;
                    }
                }

              
            }
            return null;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            // Use the RegisterPostBackControl() method to register the custom toolbar items with the ReportViewer control,
            // so that actions on these items can cause the wait control to be displayed.
            // Set export options for the report. In this example, since a static report is displayed, so the export options are always the same. The options should
            // be repopulated if the report is dynamically changed in your page.
            RenderingExtension[] extensions = ReportViewer1.LocalReport.ListRenderingExtensions();

            DropDownListExport.Items.Add(new ListItem("Export To...", "none"));

            foreach (RenderingExtension extension in extensions)
            {
                // Remove the non-visible rendering formats from the drop-down list.
                if (extension.Visible)
                    DropDownListExport.Items.Add(new ListItem(extension.LocalizedName, extension.Name));
            }

            ReportViewer1.RegisterPostBackControl(ButtonFirstPage);
            ReportViewer1.RegisterPostBackControl(ButtonPreviousPage);
            ReportViewer1.RegisterPostBackControl(TextBoxPageNumber);
            ReportViewer1.RegisterPostBackControl(ButtonNextPage);
            ReportViewer1.RegisterPostBackControl(ButtonLastPage);
            ReportViewer1.RegisterPostBackControl(ButtonBackToParent);
            ReportViewer1.RegisterPostBackControl(TextBoxFindString);
            ReportViewer1.RegisterPostBackControl(ButtonFind);
            ReportViewer1.RegisterPostBackControl(ButtonNext);
            ReportViewer1.RegisterPostBackControl(ButtonRefresh);

        }

        /// <summary>
        /// Use the PreRenderComplete event to set the states of the custom toolbar items, since all report processing and rendering is complete in the PreRender 
        /// stage. Note that this update is cached on the client side by the javascript code, so the actual update in the browser doesn't happen immediately when 
        /// the postback page is rendered. Otherwise, the toolbar might be enabled before the report actually finishes loading.
        /// </summary>
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            // Serialize the state object to the hidden field. This state object will be accessed by the javascript 
            // updateToolBarState() function.
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ToolBarSerializedState.Value = serializer.Serialize(GetToolBarState());
        }

        /// <summary>
        /// Called by Page_PreRenderComplete to retrieve the toolbar state that can be serialized and sent to the client side.
        /// </summary>
        /// <returns>An object that contains the toolbar state</returns>
        private ToolBarState GetToolBarState()
        {
            ToolBarState state = new ToolBarState();

            // If the report area contains a report page, it means that a report is successfully
            // processed and rendered. Set the appropriate toolbar items
            if (ReportViewer1.ReportAreaContentType == ReportAreaContent.ReportPage)
            {
                state.PageNumberString = ReportViewer1.CurrentPage.ToString();

                // Set states for first/previous page buttons
                if (ReportViewer1.CurrentPage > 1)
                {
                    state.IsButtonFirstPageEnabled = true;
                    state.IsButtonPreviousPageEnabled = true;
                }

                // Set label for total pages
                PageCountMode mode;
                int totalpages = ReportViewer1.LocalReport.GetTotalPages(out mode);
                LabelTotalPages.Text = String.Format("{0} {1}{2}",
                                                     "of",
                                                     totalpages,
                                                     (mode == PageCountMode.Estimate) ? "?" : String.Empty);

                // Set states for next/last page buttons 
                if (ReportViewer1.CurrentPage < totalpages)
                {
                    state.IsButtonNextPageEnabled = true;
                    state.IsButtonLastPageEnabled = true;
                }

                // Set state of back to parent button
                state.IsButtonBackToParentEnabled = ReportViewer1.LocalReport.IsDrillthroughReport;

                // Set states of Find/Next buttons
                if (ReportViewer1.SearchState == null)
                {
                    // SearchState is null. This can mean one of the following:
                    // - Search was initiated using Find button or pressing Enter on the search text box, but no hits. Disable both buttons and clear the search text box
                    // - Search was continued using Next button but no hits. Disable both buttons and clear the search text box
                    // - Search was not initiated. Will handle it separately to check the search text box.
                    state.FindString = String.Empty;
                }
                else
                {
                    // SearchState is not null. There's an existing search operation.
                    state.IsButtonFindEnabled = true;
                    state.IsButtonNextEnabled = true;
                    state.FindString = ReportViewer1.SearchState.Text;
                }

                // If search was not initiated and search text box has text, enable the Find button.
                state.IsButtonFindEnabled = (state.FindString.Length > 0);

                // Set state of Print button
                state.IsButtonPrintEnabled = true;

                state.WidthReport = ReportViewer1.LocalReport.GetDefaultPageSettings().PaperSize.Width;
                state.HeightReport = ReportViewer1.LocalReport.GetDefaultPageSettings().PaperSize.Height;
            }

            // If the report area contains something (Error or ReportPage), it means that there is a report to process.
            // Set Refresh button state accordingly. Note that if report area content is not None, the only possible states 
            // are ReportPage and Error since in all processing and rendering is complete at this stage of the page life 
            // cycle.
            state.IsButtonRefreshEnabled = (ReportViewer1.ReportAreaContentType != ReportAreaContent.None);
            
            return state;
        }

        protected void ButtonFirstPage_Click(object sender, ImageClickEventArgs e)
        {
            // Go to the first page
            ReportViewer1.CurrentPage = 1;
        }

        protected void ButtonPreviousPage_Click(object sender, ImageClickEventArgs e)
        {
            // Go to the previous page.
            try
            {
                ReportViewer1.CurrentPage -= 1;
            }
            catch (ArgumentOutOfRangeException) // thrown if integer < 1
            {
                // Show a message box in the browser.
                ScriptManager.RegisterStartupScript(ButtonPreviousPage, this.GetType(), "Page0Requested", @"<script type='text/javascript'>alert('Already at page 1.');</script>", false);
            }
        }

        /// <summary>
        /// The TextBoxPageNumber control is set to only cause a postback when the user presses the ENTER key. For more information, see the inspectTextBoxPageNumberKey() 
        /// function in CustomToolBar.js. 
        /// </summary>
        protected void TextBoxPageNumber_TextChanged(object sender, EventArgs e)
        {
            // Make sure the the postback is caused by pressing ENTER in the TextBoxPageNumber control before proceeding. This eliminates the cases when the user change the 
            // text without pressing ENTER and interacted with another UI in the page. For more information, see the inspectTextBoxPageNumberKey() function in CustomToolBar.js. 
            if (ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID == "TextBoxPageNumber")
            {
                int result = 0;
                bool error = false;

                // Go to the page number specified in the text box
                try
                {
                    if (Int32.TryParse(TextBoxPageNumber.Text, out result))
                        ReportViewer1.CurrentPage = result;
                    else
                        // Show a message box in the browser.
                        error = true;
                }
                catch (FormatException)             // thrown if input value is not integer
                {
                    error = true;
                }
                catch (ArgumentOutOfRangeException) // thrown if integer < 1
                {
                    error = true;
                }
                catch (InvalidOperationException)   // thrown if integer >= 1 but is not a valid page number
                {
                    error = true;
                }
                finally
                {
                    // In all the above error, restore the text box value in the view state and show a message box in the browser.
                    if (error)
                    {
                        TextBoxPageNumber.Text = ReportViewer1.CurrentPage.ToString();
                        ScriptManager.RegisterStartupScript(TextBoxPageNumber, this.GetType(), "InvalidPage", @"<script type='text/javascript'>alert('Enter a valid page number.');</script>", false);
                    }
                }
            }
        }

        protected void ButtonNextPage_Click(object sender, ImageClickEventArgs e)
        {
            // Go to the next page.
            try
            {
                ReportViewer1.CurrentPage++;
            }
            catch (InvalidOperationException) // thrown if integer >= 1 but is not a valid page number
            {
                // Show a message box in the browser.
                ScriptManager.RegisterStartupScript(ButtonNextPage, this.GetType(), "LastPageRequested", @"<script type='text/javascript'>alert('Already at the last page in the report.');</script>", false);
            }
        }

        protected void ButtonLastPage_Click(object sender, ImageClickEventArgs e)
        {
            int page;
            PageCountMode mode;

            // If PageCountMode is Actual, simply set CurrentPage to GetTotalPages(). But if PageCountMode is Estimate, 
            // set CurrentPage to the MaximumPageCount constant to indicate the last page is requested.
            page = ReportViewer1.LocalReport.GetTotalPages(out mode);

            if (mode == PageCountMode.Estimate)
                page = Microsoft.Reporting.WebForms.ReportViewer.MaximumPageCount;

            ReportViewer1.CurrentPage = page;
        }

        protected void ButtonBackToParent_Click(object sender, ImageClickEventArgs e)
        {
            // Go back to the parent report and keep the private report variable consistent.
            ReportViewer1.PerformBack();
        }
    }
    sealed class ToolBarState
    {
        public ToolBarState()
        {
            PageNumberString = String.Empty;
            FindString = String.Empty;
        }

        public bool IsButtonFirstPageEnabled
        {
            get;
            set;
        }

        public bool IsButtonPreviousPageEnabled
        {
            get;
            set;
        }

        public string PageNumberString
        {
            get;
            set;
        }

        public bool IsButtonNextPageEnabled
        {
            get;
            set;
        }

        public bool IsButtonLastPageEnabled
        {
            get;
            set;
        }

        public bool IsButtonBackToParentEnabled
        {
            get;
            set;
        }

        public string FindString
        {
            get;
            set;
        }

        public bool IsButtonFindEnabled
        {
            get;
            set;
        }

        public bool IsButtonNextEnabled
        {
            get;
            set;
        }

        public bool IsButtonRefreshEnabled
        {
            get;
            set;
        }

        public bool IsButtonPrintEnabled
        {
            get;
            set;
        }

        public int HeightReport { get; set; }

        public int WidthReport { get; set; }
    }
}